vimfandomcom-20200223-history
Searching
Comments TODO: merge with VimTip5 ---- Another thing worth knowing that is related to this: when entering a search, hit and the word under the cursor will appear in your search. This is very useful if you want to search for some variation of the word under the cursor. ---- If you have already searched anything, type /''' or '''? and use arrow up/down to get the last search patterns/commands. ---- Here's a mapping that lets */# search for the selected text in VISUAL MODE. Put this into your .vimrc file: " Search for selected text in visual mode with */# " effect: overrides unnamed register " Simplest version: vnoremap * y/" " Better one: vnoremap * y/\V=escape(@@,"/\\") " This is so far the best, allowing all selected characters and multiline selection: " Atom \V sets following pattern to "very nomagic", i.e. only the backslash has special meaning. " As a search pattern we insert an expression (= register) that " calls the 'escape()' function on the unnamed register content '@@', " and escapes the backslash and the character that still has a special " meaning in the search command (/|?, respectively). " This works well even with (no need to change ^I into \t), " but not with a linebreak, which must be changed from ^M to \n. " This is done with the substitute() function. vnoremap * y/\V=substitute(escape(@@,"/\\"),"\n","\\\\n","ge") vnoremap # y?\V=substitute(escape(@@,"?\\"),"\n","\\\\n","ge") ---- here's a tweak of that which works in --SELECT-- mode: vnoremap y/\V=substitute(escape(@@,"/\\"),"\n","\\\\n","ge")gV vnoremap y?\V=substitute(escape(@@,"?\\"),"\n","\\\\n","ge")gV (note that i map the commands to META-/ and META-? instead of * and # because i want to keep the option of replacing the selection with */#.) ---- means 'Hold down CTRL, press R, press W, release CTRL'. Or, verbally, Control R, Control W. ---- you can also jump backwards after a * by using as in control-o. control-i will let you jump forward again. You guessed it, it is a jump stack. also I toggle highlighting when doing a search, and turn it off when I have stopped searching. The next search turns it back on again. from my _vimrc " don't leave highlighting on for ever, but turn back when a new search " starts or the last one is repeated nnoremap :se nohls nnoremap n :set hlsn nnoremap N :set hlsN nnoremap * :set hls* nnoremap # :set hls# nnoremap / :set hls/ nnoremap ? :set hls? ---- Your mapping is not necessary, except F7. Instead of using ":set noh" use ":noh". Rest is done automaticaly. ---- From my .vimrc : " Clears search highlighting by just hitting a return. " The clears the command line. " (From Zdenek Sekera zs@sgi.com on the vim list.) " I added the final to restore the standard behaviour of " to go to the next line :nnoremap :nohlsearch/ I like this approach: is more likely than most other keys, at least when in normal mode, and you don't loose any key with this approach. ---- How would I use the '*' command to search for an word that has '-' characters within it. I find it only searches for the partial word delimited by '-' ---- set iskeyword to include '-' and the dash will be considered as a part of the word. ---- On UK keyboards, -<3> works just like # for searching backwards. That's because -<3> gives # on most other countries keyboards, although it gives the pound currency symbol on UK keyboards. Touch typists should appreciate this because it is the left-hand equivalent of how you type * with -<8>, whereas the # on UK keyboards is way over next to the return key. ---- I like to have my searches visible most of the time but in order to clear the searches only when I want to I mapped F3 to clear the search buffer. :nmap :let @/="" ---- My favourite easy-to-remember way of removing search hilight is Alt-/ " Alt / removes search highlight nnoremap ? :nohlsearch When you have searched for a particular word using *, you would like to replace the * highlighted words, then you can use :%s//new/g / carries the highlighed buffer. Similarly, with global command, :g/ will give you all the matches of the * searched pattern. For the same reason as, / carries the last highlighed buffer and print goes by default with :global command. ---- Another way to save lot of typos: Figure out how many times do we type the same word in a file. Here is a way to automate that: Once you have typed a word like "my_big_variable_name_followed_by_suffixes" then when you want to type it again, in insert mode, just type m and then hit 'p', while holding ctrl key. It will show you all the matching word in current file one-by-one that matches the word. Once you have found the word, release the ctrl p. If you have lot of words starting with 'm', you can start with 'my' and so on. PS: I had posted this tip before but since this turns to the nice thread , thought of dropping here again. ---- Another tip: the * without n I find it very useful to highlight the word under the cursor like *, but _without_ jumping directly to the next match. This way, you can quickly see the highlights in the current page without scrolling anything, and that's something you might want to do often! Then you can move to the first (ggn), last (GN), next (n) or previous (N) match as usual. The basic command is: :let @/="" And I have set it as follows in my .vimrc : map :set hls:let @/="" map :nohls ...this way, highlights and unhighlights. ---- I've always found hlsearch distracting, but when francoissteinmetz posted his and maps for highlighting the current word, I decided to change it slightly so that F10 toggles the hlsearch setting. This means F11 is not used, which frees it for some other map. Here is my toggle version of francoissteinmetz's map, map :set invhls:let @/=""/ The 'inv' prefix on a boolean setting toggles it. The trailing '/' is from another posting in this tip; it just clears the clutter left in the : command area. Just position the cursor to an interesting word, press F10, and all occurences of that word are highlighted. Use commands like n and N to search up and down. When you're done, press F10 again and the highlighting is off. It's helpful if you think of F10 as toggling the "Highlight" search mode. ---- Category:Searching